﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Raovat_UpdateTinTuDong
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            BindToGrid(WeekdayName(Weekday(Now.Date)))

        End If
    End Sub
    Protected Sub BindToGrid(ByVal _DateOfWeek As String)

        Using _data As New VSEntities
            Dim p As List(Of VSModel.AutoUpdate)
            lblTongSo.Text = _data.AutoUpdates.Count

            Dim a = _data.uspRaoVatUpTuDong(WeekdayName(Weekday(Now.Date)))
            If _DateOfWeek = "Monday" Then
                p = (From q In _data.AutoUpdates Where q.Monday = True Select q).Take(500).ToList
                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If

            If _DateOfWeek = "Tuesday" Then
                p = (From q In _data.AutoUpdates Where q.Tuesday = True Select q).ToList

                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If

            If _DateOfWeek = "Wednesday" Then
                p = (From q In _data.AutoUpdates Where q.Wednesday = True Select q).ToList

                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If

            If _DateOfWeek = "Thursday" Then
                p = (From q In _data.AutoUpdates Where q.Thursday = True Select q).ToList

                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If
            If _DateOfWeek = "Friday" Then
                p = (From q In _data.AutoUpdates Where q.Friday = True Select q).ToList

                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If

            If _DateOfWeek = "Saturday" Then
                p = (From q In _data.AutoUpdates Where q.Saturday = True Select q).ToList

                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If

            If _DateOfWeek = "Sunday" Then
                p = (From q In _data.AutoUpdates Where q.Sunday = True Select q).ToList

                Dim strKey_Name() As String = {"RaovatId"}
                With grdShow
                    .DataSource = p
                    .DataKeyNames = strKey_Name
                    .DataBind()
                End With
            End If

        End Using

    End Sub

    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim row As VSModel.AutoUpdate = CType(e.Row.DataItem, AutoUpdate)

            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = e.Row.RowIndex + 1

            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = "../../Page/Raovat/Detail.aspx?RaovatId=" & row.RaoVatId
            hplTitle.Text = row.RaoVat.Title
            Dim lblNguoiTao As Label = CType(e.Row.FindControl("lblNguoiTao"), Label)
            lblNguoiTao.Text = row.RaoVat.User.UserName
            Dim lblGioUp As Label = CType(e.Row.FindControl("lblGioUp"), Label)
            lblGioUp.Text = CType(row.TimeUpdate, Date).ToString("HH:mm")
            Dim chkRun As CheckBox = CType(e.Row.FindControl("chkRun"), CheckBox)
            If Not row.DateRun Is Nothing AndAlso CType(row.DateRun, Date).ToString("dd/MM/yyyy") = Now.Date.ToString("dd/MM/yyyy") Then
                chkRun.Checked = True
            Else
                chkRun.Checked = False
            End If
            Dim lblNgayUp As Label = CType(e.Row.FindControl("lblNgayUp"), Label)

            lblNgayUp.Text = WeekdayName(Weekday(Now.Date))

        End If
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Using _data As New VSEntities
            Dim a = _data.uspRaoVatUpTuDong(WeekdayName(Weekday(Now.Date)))
            Excute_Javascript("Alertbox('Update tin tự động thành công.')", Me.Page, True)
            BindToGrid(WeekdayName(Weekday(Now.Date)))
        End Using
    End Sub


    Protected Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles btnDelete.Click
        Dim intId As Integer = 0
        Dim intCount As Integer
        Dim intTotal As Integer
        Dim data As New VSEntities
        Try
            For Each item As GridViewRow In grdShow.Rows
                Dim chkItem As New CheckBox
                chkItem = CType(item.FindControl("chkItem"), CheckBox)
                If chkItem.Checked Then
                    intTotal += 1
                    intId = grdShow.DataKeys(item.RowIndex)("RaovatId").ToString
                    Dim q = (From p In data.AutoUpdates Where p.RaoVatId = intId Select p).FirstOrDefault
                    Try
                        data.AutoUpdates.DeleteObject(q)
                        data.SaveChanges()
                        intCount += 1
                        ' Insert_App_Log("Del Menu:" & q.Title & "", Function_Name.Raovat, Audit_Type.Delete, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Catch ex As Exception
                    End Try
                End If
            Next
            If intCount > 0 Then
                Excute_Javascript("Alertbox('Xóa dữ liệu thành công. " & intCount.ToString & " /" & intTotal.ToString & " record.');", Me.Page, True)
            Else
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End If
        Catch ex As Exception
            log4net.Config.XmlConfigurator.Configure()


            Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
        End Try
        BindToGrid(WeekdayName(Weekday(Now.Date)))
    End Sub
End Class
